package com.os.crm.controller;

import com.os.crm.pojo.TaskDetail;
import com.os.crm.service.TaskDetailService;
import com.os.util.Tools;
import com.os.util.page.PageRequest;
import com.os.util.page.PageResult;
import entity.Param;
import entity.Result;
import entity.StatusCode;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/taskDetail")
@RefreshScope
@Slf4j
public class TaskDetailController {
    @Autowired
    private TaskDetailService taskDetailService;
    
    @ApiOperation(value = "分页查询数据采集任务明细列表信息" , notes = "分页查询数据采集任务明细列表信息")
    @RequestMapping(value="/selectByPage", method=RequestMethod.GET)
    public Result selectByPage(@RequestParam(value="page",required = false) Integer page,
                               @RequestParam(value="pageSize",required = false) Integer pageSize
                                                              ,@RequestParam(value="task_result_id",required = false) String task_result_id
                                                              ,@RequestParam(value="task_id",required = false) String task_id
                                                              ,@RequestParam(value="ent_id",required = false) String ent_id
                               ){
        Map map = new HashMap();
        List paramList = new ArrayList();
                if(task_result_id!=null){
            paramList.add(new Param("task_result_id", task_result_id + "", "String","like"));
        }
                if(task_id!=null){
            paramList.add(new Param("task_id", task_id + "", "String","like"));
        }
                if(ent_id!=null){
            paramList.add(new Param("ent_id", ent_id + "", "String","like"));
        }
                map.put("condstr", Tools.genCondStr("", paramList));
        PageRequest pageRequest = new PageRequest();
        pageRequest.setPageNum(page);
        pageRequest.setPageSize(pageSize);
        PageResult pageResult = taskDetailService.selectByPage(pageRequest,map);
        Result result = new Result(true, StatusCode.OK,"查询成功",pageResult.getContent());
        result.setTotalCount(pageResult.getTotalSize());
        return result;
    }
    @ApiOperation(value = "按照ID查询数据采集任务明细信息" , notes = "按照ID查询数据采集任务明细信息")
    @ApiImplicitParam(name = "task_detail_id",value = "ID",required = true,dataType = "int")
    @RequestMapping(value="/selectById/{task_detail_id}", method=RequestMethod.GET)
    public Result selectById(@PathVariable int task_detail_id){
        return new Result(true, StatusCode.OK,"查询成功", taskDetailService.selectById(task_detail_id));
    }
    @ApiOperation(value = "新增数据采集任务明细信息" , notes = "新增数据采集任务明细信息")
    @RequestMapping(value="/add",method = RequestMethod.POST)
    public Result insert(@RequestBody TaskDetail taskDetail){
        taskDetailService.insert(taskDetail);
        return new Result(true, StatusCode.OK,"添加成功");
    }
    @ApiOperation(value = "更新数据采集任务明细信息" , notes = "更新数据采集任务明细信息")
    @RequestMapping(value="/update",method = RequestMethod.POST)
    public Result update(@RequestBody TaskDetail taskDetail){
        taskDetailService.update(taskDetail);
        return new Result(true, StatusCode.OK,"修改成功", taskDetail);
    }
    @ApiOperation(value = "删除数据采集任务明细信息" , notes = "删除数据采集任务明细信息")
    @RequestMapping(value="/{task_detail_id}",method = RequestMethod.DELETE)
    public Result delete(@PathVariable int task_detail_id){
        taskDetailService.deleteById(task_detail_id);
        return new Result(true, StatusCode.OK,"删除成功");
    }
}